package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.AdminUserRoleDto;
import com.hxf.dto.RestaurantAdminRole;
import com.hxf.dto.RestaurentUserDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;
import com.hxf.web.vo.UserRoleVo;

@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class RestaurentUserAction extends WebActionBase {

	private static final long serialVersionUID = 1L;
	private List<RestaurentUserDto> restList;
	public List<UserRoleVo> selectRoleList;
	public RestaurentUserDto restaurentUserDto;
	
	private Integer id;
	private String account;//账户名称
	private String name;//餐厅名称
	private String phone;
	private String email;
	private String pwd;
	private String mobile;
	private String loginTime;
	private String address;
	private String parentId;
	private String rank;
	private Integer status;// 状态 0.有效  1.审核中 2.审核不通过 3.锁定 4.无效 
	private String userIds;
	private String roleIds;
	
	/**
	 * 餐厅管理员信息
	 * @return
	 */
	public String restaurentUserList(){
		
		try{
			RestaurentUserDto rest = new RestaurentUserDto();
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			//餐厅登陆，只显示该餐厅
			if(null != restaurentUserDto){
				rest.setId(getRestaurentUserDto().getId());
			}
			
			ReturnData<RestaurentUserDto> rt = super.getServiceFacade().getRestaurantUserService().findByRestaurentUser(rest, targetPage, pageSize);
			restList = rt.getResultlist();
			this.setRecordCount(rt.getReturnResult());
			
		}catch(ServiceException e){
			e.printStackTrace();
		}
		
		return "restaurent-user-list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String userUpdate(){
		
		try {
			//菜单权限验证
			if(!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)){
				return "msg";
			}
			
			restaurentUserDto = (RestaurentUserDto) super.serviceFacade.getAdminUserService().get(RestaurentUserDto.class, id);
		} catch (ServiceException e){
			logger.debug(e.getMessage(), e);
		}
		return "restaurent-user-update";
	}
	
	/**
	 * 添加或者修改
	 * @return
	 */
	public String userSaveOrUpdate() {
		try {
			//菜单权限验证 
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			}
			
			RestaurentUserDto ud = new RestaurentUserDto();
			ud.setAccount(name);//用户名
			ud.setName(name);
			//ud.setUserIdNo(userIdNo);//身份证号码
			ud.setPhone(phone);//电话
			ud.setEmail(email);//邮箱
			ud.setPwd(pwd);//密码
			
			if (id != null && !"null".equals(id)) {  

				ud.setId(id);
				ud.setStatus(status);
				super.serviceFacade.getRestaurantUserService().update(ud);//修改
			} else {
				ud.setStatus(RestaurentUserDto.STATUS);//审核中 = 1
				ud.setCreateTime(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//当前时间
				super.serviceFacade.getRestaurantUserService().save(ud);//保存
			}
			
			requestURL = "restaurent-user!restaurentUserList.html";
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}

		return SUCCESS;
	}
	
	/**
	 * 餐厅管理员删除
	 * @return
	 */
	public String restaurentUserDelete(){
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			super.serviceFacade.getRestaurantUserService().delete(restaurentUserDto);
			super.println("success");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	/**
	 * 修改会员状态
	 * @return
	 */
	public String updateUserStatus() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.APP_USER_URL)) {
			return "msg";
		}  
		try {
			if ("".equals(userIds) || null == userIds || status == null) {
				println(-1);
				return null;
			}
			super.serviceFacade.getRestaurantUserService().updateUserStatus(userIds, status);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public String userAddRole() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			//restaurentUserDto = (RestaurentUserDto)super.serviceFacade.getUserService().get(RestaurentUserDto.class,id);
			RestaurentUserDto urDto = new RestaurentUserDto();
			urDto.setId(id);//点击角色绑定 id
			getSession().setAttribute("id", id);
			//AdminUserDto userDto = (AdminUserDto)getSession().getAttribute("adminUserDto");//当前登陆用户
			//AdminUserDto user = getUser();
			
			selectRoleList = super.serviceFacade.getRestaurantUserService().findByUserAddRole(urDto,id);
			
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "restaurent-user-add-role";
	}
	
	/**
	 * 保存添加角色
	 * @return
	 */
	public String userAddRoleSave() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			
			//restaurentUserDto.setId(Integer.valueOf(getSession().getAttribute("id").toString()));//点击角色绑定 id
			RestaurantAdminRole restaurentAdminRole = new  RestaurantAdminRole();
			restaurentAdminRole.setUserId(null==getSession().getAttribute("id")?null:Integer.valueOf(getSession().getAttribute("id").toString()));
			
			super.serviceFacade.getRestaurantUserService().saveUserAddRole(restaurentAdminRole, roleIds);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "restaurent-user!restaurentUserList.html";
		return SUCCESS;
	}
	
	public String userAdd(){
		return "restaurent-user-add";
	}
	
	public List<RestaurentUserDto> getRestList() {
		return restList;
	}

	public void setRestList(List<RestaurentUserDto> restList) {
		this.restList = restList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(String loginTime) {
		this.loginTime = loginTime;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getRank() {
		return rank;
	}

	public void setRank(String rank) {
		this.rank = rank;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public RestaurentUserDto getRestaurentUserDto() {
		return restaurentUserDto;
	}

	public void setRestaurentUserDto(RestaurentUserDto restaurentUserDto) {
		this.restaurentUserDto = restaurentUserDto;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public List<UserRoleVo> getSelectRoleList() {
		return selectRoleList;
	}

	public void setSelectRoleList(List<UserRoleVo> selectRoleList) {
		this.selectRoleList = selectRoleList;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
}
